using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace Lzfy_His_Service.Models
{
    #region 请求模型

    /// <summary>
    /// 授权URL请求
    /// </summary>
    public class AuthUrlRequest
    {
        /// <summary>
        /// 重定向URI
        /// </summary>
        [Url(ErrorMessage = "重定向URI格式不正确")]
        public string? RedirectUri { get; set; }
        
        /// <summary>
        /// 状态参数
        /// </summary>
        [StringLength(128, ErrorMessage = "状态参数不能超过128个字符")]
        public string? State { get; set; }
    }

    /// <summary>
    /// Senparc企业微信登录请求
    /// </summary>
    public class SenparcWeChatLoginRequest
    {
        /// <summary>
        /// 授权码
        /// </summary>
        [Required(ErrorMessage = "授权码不能为空")]
        [StringLength(512, ErrorMessage = "授权码长度不能超过512个字符")]
        public string Code { get; set; } = string.Empty;
        
        /// <summary>
        /// 状态参数
        /// </summary>
        [StringLength(128, ErrorMessage = "状态参数不能超过128个字符")]
        public string? State { get; set; }
    }

    /// <summary>
    /// JS-SDK配置请求
    /// </summary>
    public class JsApiConfigRequest
    {
        /// <summary>
        /// 当前页面URL
        /// </summary>
        [Required(ErrorMessage = "页面URL不能为空")]
        [Url(ErrorMessage = "页面URL格式不正确")]
        public string Url { get; set; } = string.Empty;
    }

    /// <summary>
    /// 签名验证请求
    /// </summary>
    public class SignatureValidationRequest
    {
        /// <summary>
        /// 微信加密签名
        /// </summary>
        [Required(ErrorMessage = "签名不能为空")]
        public string Signature { get; set; } = string.Empty;
        
        /// <summary>
        /// 时间戳
        /// </summary>
        [Required(ErrorMessage = "时间戳不能为空")]
        public string Timestamp { get; set; } = string.Empty;
        
        /// <summary>
        /// 随机数
        /// </summary>
        [Required(ErrorMessage = "随机数不能为空")]
        public string Nonce { get; set; } = string.Empty;
        
        /// <summary>
        /// 随机字符串
        /// </summary>
        public string? Echostr { get; set; }
    }

    #endregion

    #region 响应模型

    /// <summary>
    /// 授权URL响应
    /// </summary>
    public class AuthUrlResponse
    {
        /// <summary>
        /// 授权URL
        /// </summary>
        public string AuthUrl { get; set; } = string.Empty;
        
        /// <summary>
        /// 状态参数
        /// </summary>
        public string State { get; set; } = string.Empty;
        
        /// <summary>
        /// 重定向URI
        /// </summary>
        public string RedirectUri { get; set; } = string.Empty;
        
        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime ExpiresAt { get; set; }
    }

    /// <summary>
    /// JS-SDK配置响应
    /// </summary>
    public class JsApiConfigResponse
    {
        /// <summary>
        /// 企业ID
        /// </summary>
        public string CorpId { get; set; } = string.Empty;
        
        /// <summary>
        /// 应用ID
        /// </summary>
        public string AgentId { get; set; } = string.Empty;
        
        /// <summary>
        /// 时间戳
        /// </summary>
        public long Timestamp { get; set; }
        
        /// <summary>
        /// 随机字符串
        /// </summary>
        public string NonceStr { get; set; } = string.Empty;
        
        /// <summary>
        /// 签名
        /// </summary>
        public string Signature { get; set; } = string.Empty;
        
        /// <summary>
        /// JS接口列表
        /// </summary>
        public string[] JsApiList { get; set; } = Array.Empty<string>();
    }

    /// <summary>
    /// 企业微信用户信息响应
    /// </summary>
    public class WeChatUserInfoResponse
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public string UserId { get; set; } = string.Empty;
        
        /// <summary>
        /// 用户名
        /// </summary>
        public string Name { get; set; } = string.Empty;
        
        /// <summary>
        /// 手机号
        /// </summary>
        public string Mobile { get; set; } = string.Empty;
        
        /// <summary>
        /// 部门ID列表
        /// </summary>
        public int[] Department { get; set; } = Array.Empty<int>();
        
        /// <summary>
        /// 职位
        /// </summary>
        public string Position { get; set; } = string.Empty;
        
        /// <summary>
        /// 头像URL
        /// </summary>
        public string Avatar { get; set; } = string.Empty;
        
        /// <summary>
        /// 邮箱
        /// </summary>
        public string Email { get; set; } = string.Empty;
    }

    #endregion

    #region 服务层模型

    /// <summary>
    /// 企业微信用户信息结果
    /// </summary>
    public class WeChatUserInfoResult
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public string UserId { get; set; } = string.Empty;
        
        /// <summary>
        /// 用户名
        /// </summary>
        public string name { get; set; } = string.Empty;
        
        /// <summary>
        /// 设备ID
        /// </summary>
        public string DeviceId { get; set; } = string.Empty;
        
        /// <summary>
        /// 用户票据
        /// </summary>
        public string user_ticket { get; set; } = string.Empty;
        
        /// <summary>
        /// 过期时间
        /// </summary>
        public int expires_in { get; set; }
    }

    /// <summary>
    /// 企业微信成员信息
    /// </summary>
    public class WeChatMemberInfo
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public string userid { get; set; } = string.Empty;
        
        /// <summary>
        /// 用户名
        /// </summary>
        public string name { get; set; } = string.Empty;
        
        /// <summary>
        /// 手机号
        /// </summary>
        public string mobile { get; set; } = string.Empty;
        
        /// <summary>
        /// 部门ID列表
        /// </summary>
        public int[] department { get; set; } = Array.Empty<int>();
        
        /// <summary>
        /// 职位
        /// </summary>
        public string position { get; set; } = string.Empty;
        
        /// <summary>
        /// 头像URL
        /// </summary>
        public string avatar { get; set; } = string.Empty;
        
        /// <summary>
        /// 邮箱
        /// </summary>
        public string email { get; set; } = string.Empty;
        
        /// <summary>
        /// 性别
        /// </summary>
        public string gender { get; set; } = string.Empty;
        
        /// <summary>
        /// 状态
        /// </summary>
        public int status { get; set; }
        
        /// <summary>
        /// 是否启用
        /// </summary>
        public int enable { get; set; }
        
        /// <summary>
        /// 别名
        /// </summary>
        public string alias { get; set; } = string.Empty;
        
        /// <summary>
        /// 地址
        /// </summary>
        public string address { get; set; } = string.Empty;
        
        /// <summary>
        /// 扩展属性
        /// </summary>
        public object extattr { get; set; } = new();
    }

    /// <summary>
    /// JS-SDK配置信息
    /// </summary>
    public class JsApiConfig
    {
        /// <summary>
        /// 企业ID
        /// </summary>
        public string CorpId { get; set; } = string.Empty;
        
        /// <summary>
        /// 应用ID
        /// </summary>
        public string AgentId { get; set; } = string.Empty;
        
        /// <summary>
        /// 时间戳
        /// </summary>
        public long Timestamp { get; set; }
        
        /// <summary>
        /// 随机字符串
        /// </summary>
        public string NonceStr { get; set; } = string.Empty;
        
        /// <summary>
        /// 签名
        /// </summary>
        public string Signature { get; set; } = string.Empty;
    }

    /// <summary>
    /// 发送消息结果
    /// </summary>
    public class SendMessageResult
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }
        
        /// <summary>
        /// 消息ID
        /// </summary>
        public string MessageId { get; set; } = string.Empty;
        
        /// <summary>
        /// 错误代码
        /// </summary>
        public string ErrorCode { get; set; } = string.Empty;
        
        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; set; } = string.Empty;
    }

    /// <summary>
    /// 认证服务登录结果
    /// </summary>
    public class AuthServiceLoginResult
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }
        
        /// <summary>
        /// 访问令牌
        /// </summary>
        public string Token { get; set; } = string.Empty;
        
        /// <summary>
        /// 刷新令牌
        /// </summary>
        public string RefreshToken { get; set; } = string.Empty;
        
        /// <summary>
        /// 过期时间（秒）
        /// </summary>
        public int ExpiresIn { get; set; }
        
        /// <summary>
        /// 错误消息
        /// </summary>
        public string Message { get; set; } = string.Empty;
    }

    #endregion

    #region 缓存模型

    /// <summary>
    /// AccessToken缓存信息
    /// </summary>
    public class AccessTokenCache
    {
        /// <summary>
        /// 访问令牌
        /// </summary>
        public string AccessToken { get; set; } = string.Empty;
        
        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime ExpiresAt { get; set; }
        
        /// <summary>
        /// 是否过期
        /// </summary>
        [JsonIgnore]
        public bool IsExpired => DateTime.UtcNow >= ExpiresAt;
    }

    /// <summary>
    /// JsApiTicket缓存信息
    /// </summary>
    public class JsApiTicketCache
    {
        /// <summary>
        /// JS API票据
        /// </summary>
        public string Ticket { get; set; } = string.Empty;
        
        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime ExpiresAt { get; set; }
        
        /// <summary>
        /// 是否过期
        /// </summary>
        [JsonIgnore]
        public bool IsExpired => DateTime.UtcNow >= ExpiresAt;
    }

    /// <summary>
    /// 用户信息缓存
    /// </summary>
    public class UserInfoCache
    {
        /// <summary>
        /// 用户信息
        /// </summary>
        public WeChatMemberInfo UserInfo { get; set; } = new();
        
        /// <summary>
        /// 缓存时间
        /// </summary>
        public DateTime CachedAt { get; set; }
        
        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime ExpiresAt { get; set; }
        
        /// <summary>
        /// 是否过期
        /// </summary>
        [JsonIgnore]
        public bool IsExpired => DateTime.UtcNow >= ExpiresAt;
    }

    #endregion

    #region 异常模型

    /// <summary>
    /// 企业微信配置异常
    /// </summary>
    public class WeChatConfigException : Exception
    {
        /// <summary>
        /// 错误代码
        /// </summary>
        public string ErrorCode { get; }
        
        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; }

        public WeChatConfigException(string errorCode, string errorMessage) : base(errorMessage)
        {
            ErrorCode = errorCode;
            ErrorMessage = errorMessage;
        }

        public WeChatConfigException(string errorCode, string errorMessage, Exception innerException) 
            : base(errorMessage, innerException)
        {
            ErrorCode = errorCode;
            ErrorMessage = errorMessage;
        }
    }

    /// <summary>
    /// 企业微信认证异常
    /// </summary>
    public class WeChatAuthException : Exception
    {
        /// <summary>
        /// 错误代码
        /// </summary>
        public string ErrorCode { get; }
        
        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; }

        public WeChatAuthException(string errorCode, string errorMessage) : base(errorMessage)
        {
            ErrorCode = errorCode;
            ErrorMessage = errorMessage;
        }

        public WeChatAuthException(string errorCode, string errorMessage, Exception innerException) 
            : base(errorMessage, innerException)
        {
            ErrorCode = errorCode;
            ErrorMessage = errorMessage;
        }
    }

    /// <summary>
    /// 企业微信API异常
    /// </summary>
    public class WeChatApiException : Exception
    {
        /// <summary>
        /// 错误代码
        /// </summary>
        public string ErrorCode { get; }
        
        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; }

        public WeChatApiException(string errorCode, string errorMessage) : base(errorMessage)
        {
            ErrorCode = errorCode;
            ErrorMessage = errorMessage;
        }

        public WeChatApiException(string errorCode, string errorMessage, Exception innerException) 
            : base(errorMessage, innerException)
        {
            ErrorCode = errorCode;
            ErrorMessage = errorMessage;
        }
    }

    #endregion

    #region 配置模型

    /// <summary>
    /// Senparc企业微信配置
    /// </summary>
    public class SenparcWeChatConfiguration
    {
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool Enabled { get; set; } = false;
        
        /// <summary>
        /// 企业ID
        /// </summary>
        public string CorpId { get; set; } = string.Empty;
        
        /// <summary>
        /// 应用Secret
        /// </summary>
        public string CorpSecret { get; set; } = string.Empty;
        
        /// <summary>
        /// 应用ID
        /// </summary>
        public string AgentId { get; set; } = string.Empty;
        
        /// <summary>
        /// 回调URL
        /// </summary>
        public string RedirectUri { get; set; } = string.Empty;
        
        /// <summary>
        /// 前端成功页面URL
        /// </summary>
        public string FrontendSuccessUrl { get; set; } = string.Empty;
        
        /// <summary>
        /// 前端错误页面URL
        /// </summary>
        public string FrontendErrorUrl { get; set; } = string.Empty;
        
        /// <summary>
        /// Token（用于验证签名）
        /// </summary>
        public string Token { get; set; } = string.Empty;
        
        /// <summary>
        /// EncodingAESKey（用于消息加解密）
        /// </summary>
        public string EncodingAESKey { get; set; } = string.Empty;
        
        /// <summary>
        /// 缓存配置
        /// </summary>
        public CacheConfiguration Cache { get; set; } = new();
        
        /// <summary>
        /// API超时时间（毫秒）
        /// </summary>
        public int ApiTimeoutMs { get; set; } = 30000;
        
        /// <summary>
        /// 重试次数
        /// </summary>
        public int RetryCount { get; set; } = 3;
        
        /// <summary>
        /// 是否启用详细日志
        /// </summary>
        public bool EnableDetailedLogging { get; set; } = false;
    }

    /// <summary>
    /// 缓存配置
    /// </summary>
    public class CacheConfiguration
    {
        /// <summary>
        /// AccessToken缓存时间（分钟）
        /// </summary>
        public int AccessTokenCacheMinutes { get; set; } = 110; // 微信AccessToken有效期2小时，提前10分钟刷新
        
        /// <summary>
        /// JsApiTicket缓存时间（分钟）
        /// </summary>
        public int JsApiTicketCacheMinutes { get; set; } = 110; // JsApiTicket有效期2小时，提前10分钟刷新
        
        /// <summary>
        /// 用户信息缓存时间（分钟）
        /// </summary>
        public int UserInfoCacheMinutes { get; set; } = 30;
        
        /// <summary>
        /// 是否启用分布式缓存
        /// </summary>
        public bool EnableDistributedCache { get; set; } = true;
        
        /// <summary>
        /// 缓存键前缀
        /// </summary>
        public string KeyPrefix { get; set; } = "senparc_wechat";
    }

    #endregion
}